package demo.practice.tree.depth;

import demo.practice.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class P257_2 {


    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null)
            return null;
        List<String> path = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        binaryTreePaths(path, root, sb);
        return path;

    }


    private void binaryTreePaths(List<String> path, TreeNode root, StringBuffer sb) {
//        if (root == null) {   不会出现这个分支
//            if (sb.length() > 0)
//                path.add(sb.toString());
//            else
//                return;//啥也不做
//        } else

        if (root != null) {
            if (sb.length() == 0)
                sb.append(root.val);
            else
                sb.append("->" + root.val);

            if (root.left != null) {
                binaryTreePaths(path, root.left, new StringBuffer(sb));
            }
            if (root.right != null) {
                binaryTreePaths(path, root.right, new StringBuffer(sb));
            }

            if (root.left == null && root.right == null) {
                path.add(sb.toString());
            }
        }
    }
}
